package com.hq.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hq.entity.Exam;
import com.hq.mapper.ExamMapper;
import com.hq.service.ExamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("examService")
@Slf4j
public class ExamServiceImpl implements ExamService {

    @Autowired
    private ExamMapper examMapper;

    //查询所有试卷
    @Override
    public List<Exam> findAll() {
        return examMapper.findAll();
    }

    //分页查询
    @Override
    public Map<String, Object> findByPage(int page, int size) {
        log.info("分页查询");
        Map<String,Object> map=new HashMap<>();
        Page<Object> objects = PageHelper.startPage(page, size);
        List<Exam> list = examMapper.findAll();
        map.put("list",list);
        map.put("total",objects.getTotal());
        return map;
    }

    //添加试卷
    @Override
    public int addExam(Exam exam) {
        log.info("添加试卷：{}",exam);
        return examMapper.addExam(exam);
    }

    //发布试卷
    @Override
    public int publishExam(int eid) {
        log.info("发布试卷：{}",eid);
        if(examMapper.upExam(eid)>0){
            return examMapper.downExam(eid);
        }
        return 0;
    }

    //单行删除试卷
    @Override
    public int delExam(int eid) {
        log.info("删除试卷：{}",eid);
        return examMapper.delExam(eid);
    }

    //批量删除试卷
    @Override
    public int delExams(String ids) {
        log.info("批量删除的试卷id{}",ids);
        return examMapper.delExams(ids);
    }

    //编辑试卷
    @Override
    public int editExam(Exam exam) {
        log.info("修改的试卷{}",exam);
        return examMapper.updateExam(exam);
    }
}
